package com.sgb.kjwl.viewmodel.packers;

import android.text.TextUtils;

import com.sgb.kjwl.api.APIManager;
import com.swgk.core.base.api.ICallBack;
import com.sgb.kjwl.model.entity.BiddingEntity;
import com.sgb.link.entity.PickFilterEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.util.PickSelectEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.HomeRepertory;
import com.sgb.kjwl.view.ui.fragment.packers.FindJobSearchFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
*@author liwei
*@date 2021/2/19
*description:
*/
public class FragFindJobSearchModel extends BaseViewModel {

    private FindJobSearchFragment fragment;

    private int page = 1;

    public FragFindJobSearchModel(FindJobSearchFragment fragment, HomeRepertory homeRepertory) {
        this.fragment = fragment;
        PickSelectEntity all = new PickSelectEntity();
        all.setNo("");
        all.setProjectscale("全部");
    }

    public void getBiddingList(String price, String search, String provinceNo, String cityNo, String goodsType, boolean b) {
        Map<String,Object> map = new HashMap<>();
        page = b ? page + 1 : 1;
        if (!"".equals(provinceNo) && provinceNo != null) {
            map.put("areacode", provinceNo);
        }
        if (!"".equals(cityNo) && cityNo != null) {
            map.put("city", cityNo);
        }
        int limit = 10;
        map.put("limit", limit);
        map.put("name", search);
        map.put("page", page);
        map.put("state", "");
        map.put("type", "2");
        if (!"".equals(price) && price != null) {
            map.put("price", price);
        }
        if (!TextUtils.isEmpty(goodsType)) {
            map.put("goodsType", goodsType);
        }
        map.put("sortMethod", "1");
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getBiddingList(map).enqueue(new ICallBack<BaseEntity<ListResponse<List<BiddingEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<BiddingEntity>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data == null || data.getState() == null) {
                    return;
                }
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    if (data.getData() != null) {
                        fragment.showFindJobList(data.getData().getList(), b, data.getData().isLastPage());
                    }
                }
            }
        });
    }

    /**
     * 新增类型过滤接口
     * @param category
     */
    public void getTenderGoodsCategoryAllList(String category) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        Map<String,Object> map = new HashMap<>();
        map.put("type", category);
        APIManager.getInstance().getPackerAPI().getTenderGoodsCategoryAllList(map).enqueue(new ICallBack<BaseEntity<List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PickFilterEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data == null || data.getState() == null) {
                    return;
                }
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.showCategoryPop(data.getData());
                }
            }
        });
    }

    public void getPickTimes() {
        APIManager.getInstance().getPickAPI().getPickTimes().enqueue(new ICallBack<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PickSelectEntity>> data) {
                DialogHelper.dismissProgressDialog();
                if (data == null || data.getState() == null) {
                    return;
                }
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.showTimePopWindow(data.getData());
                }
            }
        });
    }

    public void getPriceData() {
        APIManager.getInstance().getPickAPI().getProjectMoney().enqueue(new ICallBack<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PickSelectEntity>> data) {
                DialogHelper.dismissProgressDialog();
                if (data == null || data.getState() == null) {
                    return;
                }
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.showPricePopWindow(data.getData());
                }
            }
        });
    }

    public void getUrlSetting(String id) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting("competitivePurchase").enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                if (data == null || data.getState() == null) {
                    return;
                }
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    String url = data.getData().getUrl() + "?id=" + id;
                    String lng = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LONGITUDE_ID, "");
                    String lat = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LATITUDE_ID, "");
                    url = url + "&lng=" + lng + "&lat=" + lat;
                    MLog.d("api", "H5跳转链接  " + url);
                    fragment.getUrl(url, id);
                } else {
                    MToast.showToast(fragment.getContext(), data.getMsg());
                }
            }
        });
    }

    // -------------------------------- 省市区 --------------------------------

    public void getCityList() {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPackerAPI().getTwoCityList().enqueue(new Callback<BaseEntity<List<PickFilterEntity<List<PickFilterEntity>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickFilterEntity<List<PickFilterEntity>>>>> call, Response<BaseEntity<List<PickFilterEntity<List<PickFilterEntity>>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() != null && response.body().getData().size() > 0) {
                        fragment.showCityPop(response.body().getData());
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickFilterEntity<List<PickFilterEntity>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
